<?php
@ob_start();
require("../../realtrack/includes/config.inc.php"); 
require("../../realtrack/includes/Database.class.php"); 
require("../../realtrack/includes/GPSFunction.php"); 

//print_r($_REQUEST);
//http://72.232.217.94/lc.php?receivedon=54999&amp;mobile=919999477288&amp;Message=aj
//http://www.chekhra/?mobilenumber=9949090472&Message=CHD MH06AQ1949&receivedon=XXXX
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect(); 

function insertMeterData($devData)
{
	$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
	$db->connect(); 
	$devData = explode("#",$devData);
	$getLiveData = "SELECT * FROM tb_livegps_info WHERE tli_imei = '".$devData[0]."'";
	$exeLiveData = $db->query($getLiveData);
	if($db->affected_rows > 0)
	{
		$fetLiveData = $db->fetch_array($exeLiveData);
		//print_r($fetLiveData);
		$odoMeter = $fetLiveData[tli_distance];
		$lastData1=explode("#",$fetLiveData[tli_parameter]);
		$lastData2=explode("@",$lastData1[0]);
		$lvData = $fetLiveData[tli_today].','.$lastData2[1];
		return $lvData;
	}
}
//echo $_SERVER['REQUEST_URI']."<br>";
if($_REQUEST[mobile]!='')
{

//$_REQUEST[Message]=urlencode($_REQUEST[Message]);
$src=explode(" ",$_REQUEST[Message]);
//echo $src[1];	http://gpsapp.shastrasoftech.com/twoway.php?mobile=#mobile#&Message=#Message# 
//$src=str_replace("%14","",$src[1]);
//$src=str_replace("%02","",$src);

	if($src[1])
	{
		$getVehi="SELECT di_id,ci_clientName,di_deviceId,di_deviceName,di_imeiId,ci_clientId FROM tb_deviceinfo,tb_clientinfo WHERE ci_id = di_clientId AND (di_deviceId='".$src[1]."' or di_deviceName='".$src[1]."') AND di_status=1";
		$resVehi=$db->query($getVehi);
		if($db->affected_rows > 0 )
		{
			$fetVehi=$db->fetch_array($resVehi);
			//print_r($fetVehi);
			$data = insertMeterData($fetVehi[di_imeiId]."#".$fetVehi[di_odoMeter]);
			
			if($fetVehi[di_deviceName] != "")
				$devName = $fetVehi[di_deviceName];
			else
				$devName = $fetVehi[di_deviceId];
				
			PrintKMLFolder($data,$devName,$fetVehi[ci_clientId],$fetVehi[di_id]);
			insertData($_REQUEST[mobile]);
		}
		else
		{
			print_r("Invalid data.");
			$mobile = "Shastra";
			$to = $_REQUEST[mobile];
			insertData($_REQUEST[mobile]);
			//sendLocation($mobile,$to,$Message,$fetVehi[ci_clientId]);
		}
	}else insertData($_REQUEST[mobile]);
}	
else
{
	print_r("Parameter missing. Please check the data once.");
	$mobile = "";
	$to = $_REQUEST[mobile];
	insertData($_REQUEST[mobile]);
	//sendLocation($mobile,$to,$Message);
	//echo "Invalid";
}

function insertData($imei)
{
	$folder="SMSData/";
	if(!is_dir($folder))
	{
		mkdir($folder, 0777);
		chmod($folder, 0777);
	}
	$subFolder=$folder.date("d-m-Y")."/";
	if(!is_dir($subFolder))
	{
		mkdir($subFolder, 0777);
		chmod($subFolder, 0777);
	}
	
	$strFilename = $subFolder.$imei;
	$myFile = $strFilename.".txt";
	$fh = @fopen($myFile, 'a') or die("can't open file");
	$stringData = $_REQUEST[mobile]." ".$_REQUEST[message]." ".date("h:i:s A")." ".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."#";
	@fwrite($fh, $stringData);
	@fclose($fh);
	
}

function PrintKMLFolder($resData,$vehiName,$resellID,$devId)
{
	if($resData)
	{
		$dStatus = explode(",",$resData);
		$res2=simpleGeocode(calLat($dStatus[3]),calLong($dStatus[2]));
		$locat=str_replace('"',"",$res2);
		
		
		$smsMsg = "Dear Client! ".$vehiName." is located at ".$locat." on ".$dStatus[9].' '.date("h:i:s A",strtotime($dStatus[10])).", Sd = ".$dStatus[4]." k/h TD = ".round($dStatus[0],2)." km - VTS Alert.";
		
		sendLocation("REALTRAK",$_REQUEST[mobile],$smsMsg,$resellID,$devId);
	}
	else
	{
		//return false;
		print_r("Dear Client! ".$vehiName." is not avilable today - VTS Alert.");
		$mobile = "";
		$to = $_REQUEST[mobile];
	}


}
function sendGatewaySMS($from,$to,$message,$uri,$username,$password,$senderid)
{
	$mobilenumbers=$to; //enter Mobile numbers
	$message = urlencode($message);
	$sender = urlencode($sender); //Your senderid
	
	$uri = str_replace("_U_",$username,$uri);
	$uri = str_replace("_P_",$password,$uri);
	$uri = str_replace("_S_",$senderid,$uri);
	$uri = str_replace("_T_",$to,$uri);
	$uri = str_replace("_M_",$message,$uri);
	//echo $uri;
	$data = @file_get_contents ($uri); 
	//echo $data;

	if(empty($data))
	{
		 return 0;
	}
	else
	{
		 return 1;
	} 
}
function sendLocation($mobile,$to,$smsMsg,$resellID,$devId)
{
	$getReseller = "select ci_smsGatewayUri,ci_smsGatewayUsername,ci_smsGatewayPassword,ci_smsGatewaySenderId from tb_clientinfo where ci_id = ".$resellID;
	$resReseller = mysql_query($getReseller);
	if(@mysql_affected_rows() > 0)
	{
		$fetReseller = @mysql_fetch_assoc($resReseller);
		//print_r($fetReseller);
		$res=sendGatewaySMS($mobile,$to,$smsMsg,$fetReseller[ci_smsGatewayUri],$fetReseller[ci_smsGatewayUsername],$fetReseller[ci_smsGatewayPassword],$fetReseller[ci_smsGatewaySenderId]);
		if($res)
		{
			$sql = "INSERT INTO tb_smsinfo(tsi_mobileno,tsi_message,tsi_tgai_id,tsi_smsResult,tsi_smsType,tsi_deviceId) VALUES('".$to."','".addslashes($smsMsg)."',-4,'".$res."','TWOWAYSMS',$devId)";
			mysql_query($sql);
		}
		else
		{
			//echo "no";
		}
	}
}


?>